package com.shanling.shanlingcontroller.persenter;



import com.shanling.shanlingcontroller.utils.SubscriptionEnable;
import com.shanling.shanlingcontroller.ui.BaseView;

import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by sinrow on 2019/2/13 0013.
 */

public class RxPresenter<T extends BaseView> implements BasePersenter<T>,SubscriptionEnable {

    private CompositeSubscription mCompositeSubscription;

    protected T mView;


    @Override
    public void addSubscription(Subscription s) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(s);
    }

    public void removeSubscription() {
        if (this.mCompositeSubscription != null && this.mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }
    }
    @Override
    public void attachView(T view) {
        this.mView = view;
    }

    @Override
    public void detachView() {
        this.mView = null;
        removeSubscription();
    }
}
